package cn.stylefeng.guns.modular.product.service.impl;

import cn.stylefeng.guns.base.pojo.page.LayuiPageFactory;
import cn.stylefeng.guns.base.pojo.page.LayuiPageInfo;
import cn.stylefeng.guns.modular.product.entity.Product;
import cn.stylefeng.guns.modular.product.mapper.ProductMapper;
import cn.stylefeng.guns.modular.product.model.params.ProductParam;
import cn.stylefeng.guns.modular.product.model.result.ProductResult;
import cn.stylefeng.guns.modular.product.service.ProductService;
import cn.stylefeng.roses.core.util.ToolUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import java.io.Serializable;
import java.util.List;

/**
 * <p>
 * 产品表 服务实现类
 * </p>
 *
 * @author jalor
 * @since 2019-10-20
 */
@Service
public class ProductServiceImpl extends ServiceImpl<ProductMapper, Product> implements ProductService {

    @Override
    public void add(ProductParam param) {
        Product entity = getEntity(param);
        this.save(entity);
    }

    @Override
    public void delete(ProductParam param) {
        this.removeById(getKey(param));
    }

    @Override
    public void update(ProductParam param) {
        Product oldEntity = getOldEntity(param);
        Product newEntity = getEntity(param);
        ToolUtil.copyProperties(newEntity, oldEntity);
        this.updateById(newEntity);
    }

    @Override
    public ProductResult findBySpec(ProductParam param) {
        return null;
    }

    @Override
    public List<ProductResult> findListBySpec(ProductParam param) {
        return null;
    }

    @Override
    public LayuiPageInfo findPageBySpec(ProductParam param) {
        Page pageContext = getPageContext();
        IPage page = this.baseMapper.customPageList(pageContext, param);
        return LayuiPageFactory.createPageInfo(page);
    }

    /**
     * 获取产品集合
     *
     * @param code 编号
     * @param name 名称
     * @return 产品集合
     */
    @Override
    public List<Product> list(String code, String name) {
        return this.baseMapper.list(code, name);
    }

    private Serializable getKey(ProductParam param) {
        return param.getId();
    }

    private Page getPageContext() {
        return LayuiPageFactory.defaultPage();
    }

    private Product getOldEntity(ProductParam param) {
        return this.getById(getKey(param));
    }

    private Product getEntity(ProductParam param) {
        Product entity = new Product();
        ToolUtil.copyProperties(param, entity);
        return entity;
    }

}
